/**
 * Created by dukes on 2015/12/4.
 */
class ChatMgr{
    public static TYPE_WORLD = 1;
    public static TYPE_SYSTEM = 2;
    //黑名单
    public blockList:any;
    //私聊列表
    public chatList:any[];
    //私聊信息记录
    public chatMap ;
    //世界聊天信息
    public worldChatInfoList:any[];

    public storeKey:string;
    //系统信息
    //public systemInfoList:any[];
    public constructor(){
        this.storeKey = "chatlist-"+Model.me.id;
        this.init();
    }

    private init(){
        var chatList = egret.localStorage.getItem(this.storeKey);
        if( chatList){
            this.chatList = JSON.parse(chatList);
        }else{
            this.chatList = [];
        }
        this.chatMap = {};
        this.worldChatInfoList = [];
        //this.systemInfoList = [];
    }


    ///=====私聊相关=====////
    public removeChater(id){
        if( this.chatMap[id])
            delete  this.chatMap[id];

        var len = this.chatList.length;
        for(var i = 0; i < len; i++){
            if( this.chatList[i].userId == id){
                this.chatList.splice(i,1);
                break;
            }
        }
    }
    public getChatInfoByUser(userId){
        return this.chatMap[userId] || [];
    }
    public addChatToUser(message,toUser,sayUser,time){
        var tgtUser = Model.me.id == toUser ? sayUser : toUser;
        if( !this.chatMap[tgtUser] )
            this.chatMap[tgtUser] = [];
        this.chatMap[tgtUser].push({message:message,userId:sayUser,time:Model.SYSTIME});
        if(PersonalChatScene.getInstance().chater && PersonalChatScene.getInstance().chater.userId == tgtUser){
            PersonalChatScene.getInstance().addChatItem({message:message,userId:sayUser,time:Model.SYSTIME});
        }
    }
    public addChatInfo(data){
        data.time = Model.SYSTIME;
        this.addChatList(data);
        this.addChatToUser(data.message,Model.me.id,data.userId,Model.SYSTIME);
    }

    public addChatList(data){
        var len = this.chatList.length;
        for(var i = 0; i < len; i++){
            if( this.chatList[i].userId == data.userId){
                data["time"] = Model.SYSTIME;
                return;
            }
        }
        data["time"] = Model.SYSTIME;
        //this.chatList.push(data);
        this.chatList.unshift(data);
        if( this.chatList.length <= 10){
            egret.localStorage.setItem(this.storeKey,JSON.stringify(this.chatList));
        }else{
            //this.chatList.sort(function(item0,item1){
            //    if( item0.time < item1.time)
            //        return -1;
            //    else
            //        return 1;
            //});
            egret.localStorage.setItem(this.storeKey,JSON.stringify(this.chatList.slice(0,10)));
        }
        if( ChatScene.getInstance().parent)
            ChatScene.getInstance().updateChatList();
    }
    //=====黑名单相关=====//
    public addBlock(data){
        if(!this.blockList) return;
        var len = this.blockList.length;
        for(var i = 0; i < len; i++){
            if( this.blockList[i].blockUserId == data.blockUserId){
                return;
            }
        }
        this.blockList.push(data);
        Model.userExt.blockNum = this.blockList.length;
    }
    public removeFromBlock(id:number){
        var len = this.blockList.length;
        for(var i = 0; i < len; i++){
            if( this.blockList[i].blockUserId == id){
                this.blockList.splice(i,1);
                break;
            }
        }
        Model.userExt.blockNum = this.blockList.length;
        if(ChatScene.getInstance().parent)
            ChatScene.getInstance().updateBlockRelated();
    }

    public addSystemChatData(data){
        var chatData = [
            {
                text:"【系统】:",
                style:{
                    "textColor": 0xf53d3d,
                    "size":16
                }
            },
            {
                text:data.content+"\n",
                style:{
                    "textColor": 0x915a07,
                    "size":16
                }
            }
        ];
        while(chatData.length){
            var hehe = chatData.shift();
            this.worldChatInfoList.push(hehe);
        }

        while(this.worldChatInfoList.length > 100){
            this.worldChatInfoList.pop();
        }
        if( ChatScene.getInstance().parent){
            ChatScene.getInstance().onMessageBack();
        }
    }
    public addWorldChatData(data){
       var chatData = this.getTextFlowByData(data);
       while(chatData.length){
           var hehe = chatData.shift();
           this.worldChatInfoList.push(hehe);
       }
        if( ChatScene.getInstance().parent){
            ChatScene.getInstance().onMessageBack();
        }
    }
    public getTextFlowByData(data:any){
        if( data.type == ChatMgr.TYPE_WORLD){//世界
            if( data.userId != Model.me.id){
                var color = 0xff00d2;
            }else{
                color = 0x3d92f5;
            }
            var prefix = {
                text:"【"+data.name+"】:",
                style:{
                    "textColor": color,
                    "size":16,
                    "href":"event:"+data.userId+"&"+data.name
                }
            };
        }else if( data.type == ChatMgr.TYPE_SYSTEM){//系统
            var prefix1 = {
                text:"【系统】:",
                style:{
                    "textColor": 0xf53d3d,
                    "size":16
                }
            };
        }

        var msg = {
            text:data.message+"\n",
            style:{
                "textColor": 0x915a07,
                "size":16
            }
        };
        return [prefix||prefix1,msg];
    }

    public onPersonalChatback(result){
        if(result.blocked){
            ShowHint("对方已把你拉黑");
        }else if( !result.online){
            ShowHint("对方不在线");
        }
    }

    public reclaimChatItem(item:any){
        if( item instanceof  MyChatItem){
            if( this._myChatItemCache.indexOf(item) == -1)
                this._myChatItemCache.push(item);
        }else if( item instanceof  OthersChatItem){
            if( this._hisChatItemCache.indexOf(item) == -1)
                this._hisChatItemCache.push(item);
        }
    }
    private _myChatItemCache:MyChatItem[] = [];
    public getMyChatItem():MyChatItem{
        if(this._myChatItemCache.length)
            return this._myChatItemCache.pop();
        return new MyChatItem();
    }
    private _hisChatItemCache:OthersChatItem[] = [];
    public getHisChatItem():OthersChatItem{
        if(this._hisChatItemCache.length)
            return this._hisChatItemCache.pop();
        return new OthersChatItem();
    }

    private static _instance:ChatMgr;
    public static getInstance():ChatMgr{
        if( ChatMgr._instance == null)
            ChatMgr._instance = new ChatMgr();
        return ChatMgr._instance;
    }
}